library(mirt)
library(tidyverse)


vars <- c("Independence", "Obedience", "Curiosity", "Considerate")
summary(CFAdata_1$Independence)
irt_data_1 <- subset(CFAdata_1, select = vars)

irt_data_1$Independence<-irt_data_1$Independence-1
irt_data_1$Obedience<-2-irt_data_1$Obedience
irt_data_1$Curiosity<-irt_data_1$Curiosity-1
irt_data_1$Considerate<-irt_data_1$Considerate-1


group_1<-as.character(CFAdata_1$treatment )

table(group_1)

group_1 <- dplyr::recode(group_1, "2"="Control", "3"="Boy ", "5"="Girl")

mod_configural_1<-multipleGroup( data=irt_data_1, 
                                 model=1, 
                                 group= group_1 ) 

mod_configural_1
summary(mod_configural_1)
M2(mod_configural_1)


mod_metric_1<-multipleGroup( data=irt_data_1, 
                             model=1, 
                             group= group_1, 
                             invariance=c('slopes'))

mod_metric_1
summary(mod_metric_1)
M2(mod_metric_1)

mod_metricb_1<-multipleGroup( data=irt_data_1, 
                              model=1, 
                              group= group_1, 
                              invariance=c('intercepts'))

mod_metricb_1
summary(mod_metricb_1)
M2(mod_metricb_1)

anova( mod_metricb_1, mod_configural_1)
anova( mod_metric_1, mod_configural_1)

mod_scalar2_1<-multipleGroup( data=irt_data_1, 
                              model=1, 
                              group= group_1, 
                              invariance=c('slopes', 'intercepts', 'free_var','free_means')
)



mod_scalar2_1
summary(mod_scalar2_1)
M2(mod_scalar2_1)
anova( mod_scalar2_1, mod_configural_1)


mod_scalar1_1<-multipleGroup( data=irt_data_1, 
                              model=1, 
                              group= group_1, 
                              invariance=c('slopes', 'intercepts', 'free_var')
)



mod_scalar1_1
summary(mod_scalar1_1)
M2(mod_scalar1_1)

anova( mod_scalar1_1, mod_configural_1)

mod_fullconstrain_1<-multipleGroup( data=irt_data_1, 
                                    model=1, 
                                    group= group_1, 
                                    invariance=c('slopes', 'intercepts')
)



mod_fullconstrain_1
summary(mod_fullconstrain_1)
M2(mod_fullconstrain_1)

anova( mod_fullconstrain_1, mod_configural_1)


anova(mod_metricb_1, mod_configural_1) #equal slopes only
anova(mod_scalar2_1, mod_metric_1) #equal intercepts, free variance and mean
anova(mod_scalar1_1, mod_scalar2_1) #fix mean
anova(mod_fullconstrain_1, mod_scalar1_1) #fix variance


anova(mod_fullconstrain_1, mod_scalar2_1, mod_metric_1,  mod_configural_1)
anova(mod_fullconstrain_1, mod_scalar2_1, mod_metricb_1,  mod_configural_1)

#plot(mod_metric_1, type = 'trace')

